package testDemo.Test1.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static ArrayList<Student> list = new ArrayList();
    public static void main(String[] args) {
        while(true){
            System.out.println("[1] 添加\n[2] 删除\n[3] 修改\n[4] 查询\n[0] 退出");
            int n = Integer.parseInt(sc.next());
            switch(n){
                case 1:addData();break;
                case 2:deleteData();break;
                case 3:upData();break;
                case 4:selectData();break;
                case 0:
                    System.out.println("感谢您的使用");
                    System.exit(0);
                default:
                    System.out.println("您输入的编号不正确，请重新输入！");break;
            }
        }
    }

    private static void selectData() {
        if(list.isEmpty()){
            System.out.println("暂无数据！");
        }else{
            System.out.println(list);
        }
    }

    private static void upData() {
        System.out.println("请输入要修改用户的姓名");
        while(true){
            String name = sc.next();
            for(Student i:list){
                if(i.getName().equals(name)){
                    System.out.println("请输入修改后的姓名和年龄信息");
                    i.setName(sc.next());
                    i.setAge(Integer.parseInt(sc.next()));
                    System.out.println("修改完成!");
                    return;
                }
            }
            System.out.println("用户名不存在，请重新输入！");
        }
    }

    private static void deleteData() {
        System.out.println("请输入要删除的信息 [姓名]");
        while(true){
            String name = sc.next();
            if(name.matches("[0-9]")){
                System.out.println("您输入的姓名中含有数字，请重新输入");
                continue;
            }else{
                for(Student i:list){
                    if(i.getName().equals(name)){
                        list.remove(i);
                        return;
                    }
                }
                System.out.println("您输入的用户不存在，请重新输入");
            }
        }
    }

    private static void addData() {
        System.out.println("请输入要添加的信息：[姓名 年龄]");
        while(true){
            Student s = new Student(sc.next(), Integer.parseInt(sc.next()));
            if(list.indexOf(s)==0){
                System.out.println("对不起，数据已存在，请重新输入!");
                continue;
            }else{
                list.add(s);
                return;
            }
        }
    }
}
